import 'package:flutter/cupertino.dart';
import 'package:mybase/base/animate_do/FFAnimate.dart';
import 'package:mybase/base/animate_do/FFAnimate2.dart';

// 点击容器：用于控制点击的效果
class JJClickContainer extends StatefulWidget {
  Widget child;

  String? clickName;

  /// 按下时的透明度
  double? clickedAlpha;

  /// 按下时的缩放
  double? clickedScale;

  /// 缩放的毫秒数
  int? scaleMill;

  /// 是否可点击
  bool enableClick;

  JJClickContainer({
    super.key,
    required this.child,
    this.clickName,
    this.clickedAlpha,
    this.clickedScale,
    this.scaleMill,
    this.enableClick = true,
  });

  @override
  State<JJClickContainer> createState() => JJClickContainerState();
}

class JJClickContainerState extends State<JJClickContainer> {
  bool isTap = false;

  AnimationController? controller;
  var animateKey = GlobalKey();

  setTapDown(bool tapDown) {
    if (!mounted) return;
    isTap = tapDown;
    if (tapDown) {
      controller?.forward();
    } else {
      controller?.reverse();
    }
  }

  @override
  Widget build(BuildContext context) {
    Widget view = widget.child;
    return scaleWidget(context, view);
  }

  Widget scaleWidget(BuildContext context, Widget child) {
    return FFAnimate2(
      key: animateKey,
      begin: 1.0,
      end: 0,
      duration: Duration(milliseconds: widget.scaleMill ?? 100),
      manualTrigger: true,
      controller: (ctl) {
        controller = ctl;
      },
      builder: (
        AnimationController controller,
        double progress,
      ) {
        var scale = (widget.clickedScale ?? 0.98) + (1 - (widget.clickedScale ?? 0.98)) * progress;
        var alpha = (widget.clickedAlpha ?? 0.9) + (1 - (widget.clickedAlpha ?? 0.9)) * progress;
        return Transform(
          transform: Matrix4.diagonal3Values(scale, scale, 1.0),
          alignment: Alignment.center,
          child: ColorFiltered(
            colorFilter: ColorFilter.matrix(matrixList(alpha)),
            child: child,
          ),
        );
      },
    );
  }

  List<double> matrixList(double value) {
    return [value, 0, 0, 0, 0, 0, value, 0, 0, 0, 0, 0, value, 0, 0, 0, 0, 0, 1, 0];
  }
}
